<template>
  <div class="GiftManagement">
    <el-card>
      <h3><span class="tag"></span> 礼物管理</h3>
      <div class="searchBox">
        <span class="txt">礼物名称：</span>
        <el-input v-model="name" clearable size="small"></el-input>
        <span class="margin"></span>
        <span class="txt">礼物价格：</span>
        <el-input v-model="price" clearable size="small"></el-input>
        <!-- <span class="margin"></span>
        <span class="txt">活动对象：</span>
        <el-select
          class="channelselect"
          v-model="type"
          placeholder="请选分类"
          size="small"
        >
          <el-option label="主播" value="1"> </el-option>
          <el-option label="用户" value="2"> </el-option>
        </el-select> -->
        <el-button
          class="btn"
          icon="el-icon-search"
          size="small"
          @click="search"
          >查询搜索</el-button
        >
      </div>
      <div class="typeBox">
        <div>
          <el-radio-group
            v-model="type"
            @change="search"
            size="small"
            fill="#7b94d8"
          >
            <el-radio-button label="0">全部</el-radio-button>
            <el-radio-button label="1">普通礼物</el-radio-button>
            <el-radio-button label="2">公告礼物</el-radio-button>
            <el-radio-button label="3">连击礼物</el-radio-button>
          </el-radio-group>
          <!-- <span style="margin-left: 20px"></span>
          <el-radio v-model="type2" label="1">普通</el-radio>
          <el-radio v-model="type2" label="2">豪华</el-radio> -->
        </div>
        <div class="right">
          <el-button type="success" plain size="small" @click="add"
            >新增礼物</el-button
          >
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%; margin-top: 10px"
        :header-cell-style="{ background: '#f5f7fa' }"
      >
        <template slot="empty">
          <el-empty :image-size="200"></el-empty>
        </template>
        <el-table-column prop="name" label="礼物名称"> </el-table-column>
        <el-table-column prop="type" label="礼物类别">
          <template slot-scope="scope">
            {{
              scope.row.type == 1
                ? "普通礼物"
                : scope.row.type == 2
                ? "公告礼物"
                : "连击礼物"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="gold" label="价格（金币）"> </el-table-column>
        <el-table-column prop="cover" label="礼物图片">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              class="imgCover"
              :src="scope.row.cover"
              :preview-src-list="[scope.row.cover]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="path" label="礼物动画">
          <template slot-scope="scope">
            <div class="svgbox">
              <Svga
                :url="scope.row.path"
                :index="scope.$index"
                width="98px"
                height="98px"
                :border="true"
              ></Svga>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="path" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              plain
              @click="edit(scope.row)"
              >编辑</el-button
            >
            <!-- <el-button
              type="info"
              size="small"
              plain
              @click="operation(scope.row)"
              >停止</el-button
            > -->
            <el-button size="small" plain @click="del(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :page.sync="page"
        :limit.sync="pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      @close="dialogClose"
      :title="title"
      :visible.sync="show"
      width="30%"
      center
    >
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="礼物名称">
          <el-input v-model="form.name" placeholder="请输入活动名称"></el-input>
        </el-form-item>
        <el-form-item label="礼物类别">
          <el-radio-group v-model="form.type">
            <el-radio label="1">普通礼物</el-radio>
            <el-radio label="2">公告礼物</el-radio>
            <el-radio label="3">连击礼物</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="礼物图片">
          <el-upload
            class="avatar-uploader"
            :http-request="upLoad"
            :show-file-list="false"
            action
          >
            <div class="imgbox" v-if="form.cover">
              <img :src="form.cover" class="avatar" />
              <!-- <i class="el-icon-close" @click.stop="delImg"></i> -->
              <!-- <div class="txt" @click.stop="">{{ form.dialogImageUrl }}</div> -->
            </div>
            <i v-else class="upicon el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="礼物动画">
          <el-upload
            class="avatar-uploader"
            :http-request="upLoad1"
            :show-file-list="false"
            action
            style="height: 200px"
          >
            <!-- <div class="imgbox" v-if="form.dialogImageUrl">
              <img :src="form.dialogImageUrl" class="avatar" />
              <div class="txt" @click.stop="">{{ form.dialogImageUrl }}</div>
            </div> -->
            <div v-if="form.path" class="svga">
              <Svga
                :url="form.path"
                index="ksdks"
                width="178px"
                height="178px"
                :border="true"
              ></Svga>
              <!-- <div class="txt" @click.stop="">{{ form.dialogImageUrl }}</div> -->
            </div>

            <i v-else class="upicon el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="礼物价格">
          <el-input v-model="form.gold" placeholder="请输入礼物价格"></el-input>
        </el-form-item>
        <!-- <el-form-item label="礼物类别">
          <el-radio-group v-model="form.resource">
            <el-radio label="普通"></el-radio>
            <el-radio label="豪华"></el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <!-- <div id="svgaBox"></div>
      <div @click="playSvga">点击播放</div> -->
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="show = false">取 消</el-button>
        <el-button size="small" class="btn" type="primary" @click="submit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Svga from "@/components/SvgaWeb";
import Pagination from "@/components/Pagination"; // 分页
import { client } from "@/utils/oss.js";
import { ossToken } from "@/api/NotificationManagement";
import {
  giftQueryList,
  giftAddGift,
  giftDeleteGift,
} from "@/api/operationManagement";
// import SVGA from "svgaplayerweb";
export default {
  components: { Pagination, Svga },
  data() {
    return {
      loading: false,
      name: "",
      price: "",
      type: "0",
      // type1: "1",
      // type2: "",
      tableData: [],
      total: 0,
      page: 1,
      pageSize: 10,
      show: false,
      title: "",
      form: {
        id: "",
        name: "",
        type: "1",
        cover: "",
        path: "",
        gold: "",
      },
    };
  },
  methods: {
    add() {
      this.title = "新增礼物";
      this.form = {
        id: "",
        name: "",
        type: "1",
        cover: "",
        path: "",
        gold: "",
        delete_status: "1",
        sort: "1",
      };
      this.show = true;
    },
    async submit() {
      if (
        this.form.name == "" ||
        this.form.cover == "" ||
        this.form.path == "" ||
        this.form.gold == ""
      ) {
        return this.$message.error("请检查内容不能为空！");
      }
      const res = await giftAddGift({
        id: this.form.id,
        name: this.form.name,
        cover: this.form.cover,
        path: this.form.path,
        gold: this.form.gold,
        type: this.form.type,
        delete_status: this.form.delete_status,
        sort: this.form.sort,
      });
      if (res.code == 0) {
        this.$message.success("保存成功！");
        this.getList();
        this.show = false;
      }
      console.log(this.form);
    },
    edit(obj) {
      this.title = "编辑礼物";
      this.form = {
        id: obj.id,
        name: obj.name,
        type: obj.type,
        cover: obj.cover,
        path: obj.path,
        gold: obj.gold,
        delete_status: "1",
        sort: "1",
      };
      this.show = true;
    },
    del(id) {
      this.$confirm("是否删除该banner活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await giftDeleteGift({
            id,
          });
          if (res.code == 0) {
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async getList() {
      this.loading = true;
      const res = await giftQueryList({
        page: this.page,
        pageSize: this.pageSize,
        name: this.name,
        gold: this.price,
        type: this.type,
      });
      this.loading = false;
      if (res.code == 0) {
        this.tableData = res.data.list;
        this.total = res.data.total;
      }
    },
    search() {
      this.page = 1;
      this.getList();
    },
    //上传
    async upLoad(file) {
      await ossToken({
        type: 0,
      }).then((res) => {
        if (res.code == 0) {
          this.dataObj = {
            accessKeyId: res.data.accessKey,
            accessKeySecret: res.data.accessSecret,
            securityToken: res.data.token,
            region: "oss-cn-hangzhou",
            bucket: res.data.bucket,
          };
        }
      });
      const self = this;
      let fileNames =
        "/img/" +
        Math.round(new Date() / 1000) +
        "." +
        file.file.name.substring(file.file.name.lastIndexOf(".") + 1);
      console.log(fileNames);
      client(this.dataObj)
        .multipartUpload(fileNames, file.file)
        .then((result) => {
          //下面是如果对返回结果再进行处理，根据项目需要
          this.form.cover = "https://img.psychexy.com" + fileNames;
          self.$message({
            message: "上传成功",
            type: "success",
          });
        })
        .catch((err) => {
          self.$message.error("上传失败");
        });
    },
    async upLoad1(file) {
      await ossToken({
        type: 0,
      }).then((res) => {
        if (res.code == 0) {
          this.dataObj = {
            accessKeyId: res.data.accessKey,
            accessKeySecret: res.data.accessSecret,
            securityToken: res.data.token,
            region: "oss-cn-hangzhou",
            bucket: res.data.bucket,
          };
        }
      });
      const self = this;
      let fileNames =
        "/img/" +
        Math.round(new Date() / 1000) +
        "." +
        file.file.name.substring(file.file.name.lastIndexOf(".") + 1);
      console.log(fileNames);
      client(this.dataObj)
        .multipartUpload(fileNames, file.file)
        .then((result) => {
          //下面是如果对返回结果再进行处理，根据项目需要
          this.form.path = "https://img.psychexy.com" + fileNames;
          self.$message({
            message: "上传成功",
            type: "success",
          });
        })
        .catch((err) => {
          self.$message.error("上传失败");
        });
    },
    //弹窗关闭
    dialogClose() {},
    // 删除已上传图片
    // delImg() {
    //   this.form.dialogImageUrl = "";
    // },
    // playSvga() {
    //   const player = new SVGA.Player("#svgaBox");
    //   var parser = new SVGA.Parser("#svgaBox");
    //   //注意，这里必须是服务器地址，否则报错
    //   parser.load(
    //     "https://img.psychexy.com/img/1634031614.svga",
    //     function (videoItem) {
    //       //   player.loops = 1; // 设置循环播放次数是1
    //       player.setVideoItem(videoItem);
    //       player.startAnimation();
    //     }
    //   );
    // },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.GiftManagement {
  padding: 20px;
  h3 {
    margin: 0 0;
    display: flex;
    align-items: center;
    .tag {
      display: inline-block;
      width: 5px;
      height: 18px;
      background-color: #7b94d8;
      margin-right: 10px;
    }
  }
  .searchBox {
    display: flex;
    align-items: center;
    margin: 20px 0;
    .margin {
      margin: 0 10px;
    }
    .txt {
      display: inline-block;
      height: 32px;
      background-color: #f5f7fa;
      line-height: 32px;
      padding-left: 10px;
      color: #9d9399;
      border-radius: 4px 0 0 4px;
      border: 1px solid #dcdfe6;
      border-right: none;
    }
    /deep/ .avatar-uploader {
      height: 200px;
    }
    /deep/ .el-input__inner {
      border-radius: 0 4px 4px 0 !important;
    }
    .el-input {
      width: 200px;
    }
  }
  .typeBox {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .upicon {
    display: inline-block;
    width: 178px;
    height: 178px;
    line-height: 178px;
    font-size: 24px;
    border: 1px dashed #ccc;
    border-radius: 8px;
  }
  .imgbox {
    position: relative;
    .avatar {
      display: inline-block;
      width: 178px;
      height: 178px;
      line-height: 178px;
      font-size: 24px;
      border: 1px dashed #ccc;
      border-radius: 8px;
      padding: 2px;
    }
    i {
      position: absolute;
      top: -10px;
      right: 40px;
      font-size: 24px;
      font-weight: 700;
      color: #000;
    }
  }
  //   #svgaBox {
  //     width: 200px;
  //     height: 200px;
  //   }
  .imgCover {
    width: 100px;
    height: 100px;
    box-sizing: border-box;
    border: 1px dashed #ccc;
    border-radius: 5px;
  }
  .svgbox {
    padding: 2px 0;
    display: flex;
    justify-content: center;
  }
}
</style>